package com.ssm.controller;

import com.ssm.entity.User;
import com.ssm.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by shuyun on 2016/8/9.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    private static Logger logger = Logger.getLogger(UserController.class);

    @Resource
    private UserService userService;

//    @RequestMapping(value = "/", method = RequestMethod.GET)
//    public String index(Model model){
//        logger.info(".................index");
//        return "redirect:/user";
//    }

    @RequestMapping("/findUser")
    public ModelAndView findUser(){
        ModelAndView modelAndView = new ModelAndView();
        //调用service方法得到用户列表
        List<User> users = userService.findUser();
        //将得到的用户列表内容添加到ModelAndView中
        modelAndView.addObject("users",users);
        //设置响应的jsp视图
        modelAndView.setViewName("findUser");

        return modelAndView;
    }

    @RequestMapping(value = "/json")
    public @ResponseBody List<User> testStr(){
        List<User> users = userService.findUser();
        return users;
    }

    @RequestMapping(value = "/show")
    public String testShow(HttpServletRequest request,Model model){
        User user = new User();
        user.setUsername("ssssssss");
        user.setAddress("shenzhen");
        user.setSex("m");
        model.addAttribute("user",user);
        return "show";
    }

    // list page
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String showAllUsers(Model model) {
        model.addAttribute("users", userService.findUser());
        return "list";
    }


    @RequestMapping ( "/showView/{variable1}" )
    public ModelAndView showView( @PathVariable String variable1) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName( "show" );
        modelAndView.addObject( " 需要放到 model 中的属性名称 " , " 对应的属性值，它是一个对象 " );
        return modelAndView;
    }


}
